package com.ruoyi.workflow.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.utils.PageRequest;
import com.ruoyi.utils.PageResult;
import com.ruoyi.utils.PaginationHelper;
import com.ruoyi.workflow.dto.*;
import com.ruoyi.workflow.service.ICompanyApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 单位注册审批记录Controller
 *
 * @author 周志雄
 * @date 2024-03-09
 */
@Api(tags = "工作流单位审批相关API")
@RestController
@RequestMapping("/workFlow/companyApply")
public class CompanyApplyController extends BaseController {

    @Autowired
    private ICompanyApplyService companyApplyService;

    /**
     * 查询全部单位注册审批记录列表
     */
    @ApiOperation("查询单位相关申请信息记录列表")
    @PostMapping("/list")
    public AjaxResult<PageResult<CompanyApplyResDto>> list(@RequestBody CompanyApplySearchDto companyApplySearchDto){
        List<CompanyApplyResDto> list = companyApplyService.selectTaskListByType(companyApplySearchDto);
        PageRequest pageRequest = new PageRequest(companyApplySearchDto.getPageNum(), companyApplySearchDto.getPageSize());
        PageResult<CompanyApplyResDto> pageResult = PaginationHelper.paginateList(list, pageRequest);
        return AjaxResult.success(pageResult);
    }

    /**
     * 查询单位相关申请信息【初审或审查决定】记录列表
     */
    @ApiOperation("查询单位相关申请信息【初审或审查决定】记录列表")
    @PostMapping("/initialOrDecisionReviewList")
    public AjaxResult<PageResult<CompanyApplyResDto>> initialOrDecisionReviewList(@RequestBody CompanyApplySearchInitialOrDecisionReviewDto dto){
        CompanyApplySearchDto companyApplySearchDto = new CompanyApplySearchDto();
        BeanUtils.copyProperties(dto, companyApplySearchDto);
        List<CompanyApplyResDto> list = companyApplyService.selectTaskListByType(companyApplySearchDto);
        // 过滤
        List<CompanyApplyResDto> filteredList = list.stream()
                .filter(l -> dto.getTaskName().equals(l.getCurrentApprovalNodeName()))
                .collect(Collectors.toList());
        PageRequest pageRequest = new PageRequest(companyApplySearchDto.getPageNum(), companyApplySearchDto.getPageSize());
        PageResult<CompanyApplyResDto> pageResult = PaginationHelper.paginateList(filteredList, pageRequest);
        return AjaxResult.success(pageResult);
    }

    /**
     * 完成单位审批
     */
    @ApiOperation("完成单位审批")
    @PostMapping("complete")
    public AjaxResult<String> complete(@RequestBody TaskCompleteDto taskCompleteDto){
        return companyApplyService.completeTask(taskCompleteDto);
    }

    /**
     * 查看审批详情页
     * @param applyId
     * @return
     */
    @ApiOperation("进入审批页详情")
    @GetMapping("/{applyId}")
    public AjaxResult<CompanyApplyInfoDto> info(@ApiParam("业务ID") @PathVariable("applyId") String applyId){
        CompanyApplyInfoDto companyApplyInfoDto = companyApplyService.selectInfoById(applyId);
        return AjaxResult.success(companyApplyInfoDto);
    }

    /**
     * 撤销单位审批
     * @param applyId
     * @return
     */
    @ApiOperation("撤销单位审批")
    @PutMapping("rollback")
    public AjaxResult<String> rollback(String applyId){
        return companyApplyService.rollback(applyId);
    }
}
